package com.john.pplay.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.AppCompatImageButton;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.Toast;

import com.john.pplay.common.FileExploerAdapter;
import com.john.pplay.common.ItemListener;
import com.john.pplay.R;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class FileExploerActivity extends BaseActivity implements ItemListener {

    private RecyclerView recyclerView;
    private AppCompatImageButton buttonSetting;
    private FileExploerAdapter adapter;
    private List<File> listFile;
    private File currentPath;
    private int numBack;

    @Override
    protected void onCreate(Bundle bundle){
        super.onCreate(bundle);
        this.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(R.layout.activity_file_explore);

        recyclerView = (RecyclerView)findViewById(R.id.rcv_file_list);
        buttonSetting = (AppCompatImageButton)findViewById(R.id.btn_setting);
        buttonSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FileExploerActivity.this,SettingActivity.class);
                FileExploerActivity.this.startActivity(intent);
            }
        });

        currentPath = Environment.getExternalStorageDirectory();
        listFile = new LinkedList<>();

        adapter = new FileExploerAdapter(this);
        adapter.setListener(this);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);

        refresh(currentPath);
    }

    @Override
    protected void onResume(){
        super.onResume();
    }
    private void refresh(File parent){
        List<File> tmp = new ArrayList<>();
        if (parent.exists() && parent.isDirectory()){
            for (File chil : parent.listFiles()){
                if (chil.isDirectory() || isVideo(chil)) {
                    tmp.add(chil);
                }
            }
        }
        if (tmp.isEmpty()){
            Toast.makeText(this,getResources().getString(R.string.toast_empty_directory),Toast.LENGTH_SHORT).show();
            return;
        }
        listFile.clear();
        listFile.addAll(tmp);
        adapter.update(listFile);
    }
    @Override
    public void onItemClick(int position) {
        if (isVideo(listFile.get(position))) {
            Intent intent = new Intent(this,VideoActivity.class);
            intent.putExtra("videoPath",listFile.get(position).getAbsolutePath());
            this.startActivity(intent);
        }else{
            currentPath = listFile.get(position);
            refresh(currentPath);
        }
    }

    @Override
    public void onBackPressed(){
        if (currentPath.equals(Environment.getExternalStorageDirectory())) {
            numBack++;
            if (numBack < 2){
                Toast.makeText(this,getResources().getString(R.string.toast_exit),Toast.LENGTH_SHORT).show();
            }else {
                super.onBackPressed();
            }

        }else{
            numBack = 0;
            currentPath = currentPath.getParentFile();
            refresh(currentPath);
        }
    }

    public static boolean isVideo(File file){
        String name = file.getName().toLowerCase();
        if (name.endsWith(".mp4") ||
                name.endsWith(".mkv") ||
                name.endsWith(".rmvb") ||
                name.endsWith(".avi") ||
                name.endsWith(".mpeg") ||
                name.endsWith(".wmv") ||
                name.endsWith(".vob") ||
                name.endsWith(".3gp") ||
                name.endsWith(".mov") ||
                name.endsWith(".flv") ||
                name.endsWith(".f4v") ||
                name.endsWith(".m4v") ||
                name.endsWith(".rm") ||
                name.endsWith(".ts")
                )
            return true;
        else
            return false;
    }
}
